package com.oa.service.impl;

import com.oa.mapper.UserMapper;
import com.oa.pojo.User;
import com.oa.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴世荣
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2023-10-22 17:02:33
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //登陆
    @Override
    public User login(String usernameOrPhone, String password) {
        //判断usernameOrPhone和密码不为空
        if (usernameOrPhone == null || password == null) {
            User user = new User();
            return user;
        }
        //匹配电话号码的格式
        String regex = "1[0-9]{10}";
        //如果是电话号码就用电话号查询
        if (usernameOrPhone.matches(regex)) {
            User user = new User();
            user.setPhone(usernameOrPhone);
            user.setPassword(password);
            user = userMapper.login(user);
            return user;
        }
        //否则用用户名查询
        else {
            User user = new User();
            user.setUsername(usernameOrPhone);
            user.setPassword(password);
            return userMapper.login(user);
        }
    }
    //多条件查询
    @Override
    public List<User> selectAll(User user) {
        List<User> userList = new ArrayList<>();
        userList = userMapper.selectAll(user);
        return userList;
    }
    //删除
    @Override
    public int deleteUser(Integer id ){
        User user = new User();
        user.setId(id);
        return userMapper.deleteUser(user);
    }
    //修改
    @Override
    public int updateUser(User user) {
        int flag = 0;
        //拿到所有用户 userList
        User user1 = new User();
        List<User> userList = userMapper.selectAll(user1);
        for (User u : userList) {
            if (user.getId() != u.getId()) {
                if (u.getUsername().equals(user.getUsername())) {//用户名去重   -1
                    flag = -1;
                    return flag;
                } else if (u.getIdCard().equals(user.getIdCard())) {//身份证  -2
                    flag = -2;
                    return flag;
                } else if (u.getPhone().equals(user.getPhone())) {//电话号码  -3
                    flag = -3;
                    return flag;
                }
            }
        }
        //执行到这表明OK
        flag= userMapper.updateUser(user);
        return flag;
    }

    //添加
    @Override
    public int insertUser(User user) {
        int flag = 0;
        User user1 = new User();
        List<User> userList = userMapper.selectAll(user1);
        //在全部用户中遍历，避免用户名,份证，电话号码重复
        for (User u : userList) {
            if ( u.getUsername().equals(user.getUsername()) ||
                 u.getIdCard().equals(user.getIdCard()) ||
                 u.getPhone().equals(user.getPhone())) {
                flag = -1;
                return flag;
            }
        }
        //电话号11位
        if(user.getPhone().length()!=11){
            flag=-1;
            return flag;
        }//年龄18~100
        else if (user.getAge()>100||user.getAge()<18){
            flag=-1;
            return flag;
        }//身份证号18位
        else if (user.getIdCard().length()!=18){
            flag=-1;
            return flag;
        }
        System.out.println(user);
        flag= userMapper.insertUser(user);
        return flag;
    }
}




